package no.ntnu.fp.handlers;

import java.io.IOException;
import java.util.List;

import no.ntnu.fp.connection.ReceiveWorker;
import no.ntnu.fp.handler.Handler;
import no.ntnu.fp.handlers.exceptions.NetworkException;
import no.ntnu.fp.messages.Message;
import no.ntnu.fp.messages.impl.EmployeesFetchedMessage;
import no.ntnu.fp.model.Employee;
import no.ntnu.fp.net.co.Connection;
import no.ntnu.fp.services.EmployeeService;

public class GetEmployeesHandler implements Handler {

	private final EmployeeService employeeService;

	public GetEmployeesHandler(EmployeeService employeeService) {
		this.employeeService = employeeService;
	}

	@Override
	public void handleMessage(Message message) {
		List<Employee> employees = employeeService.getAllEmployees();

		Connection connection = ReceiveWorker.getThreadLocalConnection();
		
		EmployeesFetchedMessage reply = new EmployeesFetchedMessage(employees);
		
		try {
			connection.send(reply.toString());
		} catch (IOException ex) {
			throw new NetworkException("Unable to send reply", ex);
		}
	}
}
